package com.gdth.widget.webview;

import java.io.File;

import com.gdth.utils.NetUtil;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;

/**
 * WebView参数设置
 * @author Administrator
 *
 */
public class WebViewUnit{
	public static final String WebViewDB = "webview.db";
	public static final String WebViewCacheDB = "webviewCookiesChromium.db";
	private static final String APP_CACAHE_DIRNAME = "/webcache";
	
	public static void getWebSettings(Context mContext, WebView mWebView){
		WebSettings mWebSettings = mWebView.getSettings();
		
		//启用javascript  
		mWebSettings.setJavaScriptEnabled(true);
		
		//支持缩放功能
		mWebSettings.setSupportZoom(true);
		// access Assets and resources  
		mWebSettings.setAllowFileAccess(true);    
		//zoom page  
		mWebSettings.setBuiltInZoomControls(false); 
		
		//提高渲染的优先级  
		mWebSettings.setRenderPriority(RenderPriority.HIGH);
		
		//判断网络状态， 设置 缓存模式
		boolean netStatus = NetUtil.isNetworkConnected(mContext);
		if (netStatus) mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		
		else mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); 
		               
		String cacheDirPath = mContext.getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME;  
//      String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;  

		//设置数据库缓存路径
        mWebSettings.setDatabasePath(cacheDirPath);  
        //设置  Application Caches 缓存目录  
        mWebSettings.setAppCachePath(cacheDirPath);
        
        //开启 Application Caches 功能  
        mWebSettings.setAppCacheEnabled(true);	
		mWebSettings.setDatabaseEnabled(true);	
        // 开启 DOM storage API 功能  
		mWebSettings.setDomStorageEnabled(true); 
		
		//开启 Application Caches功能, 设置缓存路径  
//		mWebSettings.setAppCacheMaxSize(1024*1024*5); 
	}
	
	/** 
     * 清除WebView缓存 
     */
	public static void clearWebViewCache(Activity mActivity){  
        //清理Webview缓存数据库  
        try {  
        	mActivity.deleteDatabase("webview.db");   
        	mActivity.deleteDatabase("webviewCache.db");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
          
        //WebView 缓存文件  
        File appCacheDir = new File(mActivity.getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);  
          
        File webviewCacheDir = new File(mActivity.getCacheDir().getAbsolutePath()+"/webviewCache");  
          
        //删除webview 缓存目录  
        if(webviewCacheDir.exists()){  
            deleteFile(webviewCacheDir);  
        }  
        //删除webview 缓存 缓存目录  
        if(appCacheDir.exists()){  
        	deleteFile(appCacheDir);  
        }  
    }  
	
	public static void deleteFile(File file) {  
        if (file.exists()) {  
            if (file.isFile()) {  
                file.delete();  
            } else if (file.isDirectory()) {  
                File files[] = file.listFiles();  
                for (int i = 0; i < files.length; i++) {  
                    deleteFile(files[i]);  
                }  
            }  
            file.delete();  
        } else {  
//            Log.e(TAG, "delete file no exists " + file.getAbsolutePath());  
        }  
    } 
}
